import data.DataBase;
import data.NamePassword;
import user.*;

import java.util.ArrayList;
import java.util.Scanner;

class InvalidUsernameException extends RuntimeException {
    //帮助父类构造
    public InvalidUsernameException(String message) {
        super(message);
    }
}

class InvalidPasswordException extends RuntimeException {
    //帮助父类构造
    public InvalidPasswordException(String message) {
        super(message);
    }
}

public class Main {

    //登录注册函数：
    public static void sign(NamePassword namePassword) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1.登录 2.注册");
            System.out.print("请输入你的选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();//读取/n

            if (choice==1) {
                System.out.print("请输入你的用户名：");
                String name = scanner.nextLine();
                System.out.print("请输入你的密码：");
                String password = scanner.nextLine();
                int i=0;
                for (i = 0; i < namePassword.getNPnubmer(); i++) {
                    if (name.equals(namePassword.getNapas(i).getName())) {
                        break;
                    }
                }
                if (i==namePassword.getNPnubmer()) {
                    throw new InvalidUsernameException("用户名错误！");
                }
                int j=0;
                for (j = 0; j < namePassword.getNPnubmer(); j++) {
                    if (password.equals(namePassword.getNapas(i).getPassword())) {
                        return;
                    }
                }
                if (j==namePassword.getNPnubmer()) {
                    throw new InvalidPasswordException("密码错误！");
                }

            } else if (choice==2) {
                int currentNumber = namePassword.getNPnubmer();
                System.out.print("请输入你的用户名：");
                String name = scanner.nextLine();
                System.out.print("请输入你的密码：");
                String password = scanner.nextLine();
                namePassword.setNaPas(name,password);
                namePassword.setNPnubmer(currentNumber+1);
                System.out.println("注册成功！");
            } else {
                System.out.println("输入错误，请重新输入！");
            }
        }


    }

    //登录核酸检测信息管理系统
    public static User login(DataBase dataBase,NamePassword namePassword) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1.居民  2.采样人员  3.检测人员 0.退出系统");
        System.out.print("请输入你的身份：");
        int id=scanner.nextInt();
        scanner.nextLine();
        // 处理无效选择：
        if(id==0) {
            System.out.println("已退出核酸检测信息管理系统！");
            System.exit(0);
            return null;
        }
        if (id!=1 && id!=2 && id!=3) {
            System.out.println("无效选择，退出系统！");
            System.exit(1);
        }

        //处理登录异常异常
        try{
            sign(namePassword);
            System.out.println("登录成功！");
        } catch (InvalidUsernameException e) {
            System.out.println(e);
            System.out.println("登录异常，退出系统！");
            System.exit(1);
        } catch (InvalidPasswordException e) {
            System.out.println(e);
            System.out.println("登录异常，退出系统！");
            System.exit(1);
        }

        // 处理有效选择：
        if(id==1) {
            return new Resident();
        }
        if(id==2) {
            return new Samper();
        }
        if (id==3){
            System.out.print("请输入检测机构：");
            String station=scanner.nextLine();
            dataBase.setCheckInstation(station);
            return new Checker();
        }

        return null;
    }


    public static void main(String[] args) {
        NamePassword namePassword = new NamePassword();
        DataBase dataBase=new DataBase();
        while (true) {
            int choice=1;
            User user = login(dataBase,namePassword);
            while (choice!=0) {
                choice=user.menu();
                user.doWork(choice,dataBase);
            }

        }


    }

}
